﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Conditional"/>.
    /// </summary>
    public class ConditionalVM : OperatorVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(Conditional),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new ConditionalVM((Conditional)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ExpressionVM _ifVM;
        protected ExpressionVM _thenVM;
        protected ExpressionVM _elseVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Conditional"/>.
        /// </summary>
        public ConditionalVM(Conditional conditional, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(conditional, dictionary)
        {
            _ifVM = (ExpressionVM)CreateVM(conditional.If, false, dictionary);
            _thenVM = (ExpressionVM)CreateVM(conditional.Then, false, dictionary);
            _elseVM = (ExpressionVM)CreateVM(conditional.Else, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Conditional"/> model.
        /// </summary>
        public Conditional Conditional
        {
            get { return (Conditional)CodeObject; }
        }

        /// <summary>
        /// The 'if' expression.
        /// </summary>
        public ExpressionVM If
        {
            get { return _ifVM; }
        }

        /// <summary>
        /// The 'then' expression.
        /// </summary>
        public ExpressionVM Then
        {
            get { return _thenVM; }
        }

        /// <summary>
        /// The 'else' expression.
        /// </summary>
        public ExpressionVM Else
        {
            get { return _elseVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return Brushes.PaleTurquoise; }
        }

        public override Brush BackgroundBrush
        {
            get { return Brushes.LightCyan; }
        }

        public override void Render(CodeRenderer renderer, RenderFlags flags)
        {
            base.Render(renderer, flags | RenderFlags.ForceBorder);
        }

        public override void RenderExpression(CodeRenderer renderer, RenderFlags flags)
        {
            flags |= RenderFlags.ForceBorder;
            if (_ifVM != null)
                _ifVM.Render(renderer, flags);

            RenderFlags thenFlags = flags;
            if (_thenVM != null && _thenVM.IsFirstOnLine)
            {
                renderer.NewLine();
                thenFlags |= RenderFlags.SuppressNewLine;
                renderer.RenderText("? ", PUNC_BRUSH, this);
            }
            else
                renderer.RenderText(" ? ", PUNC_BRUSH, this);
            if (_thenVM != null)
                _thenVM.Render(renderer, thenFlags);

            if (_elseVM != null && _elseVM.IsFirstOnLine)
            {
                renderer.NewLine();
                flags |= RenderFlags.SuppressNewLine;
                renderer.RenderText(": ", PUNC_BRUSH, this);
            }
            else
                renderer.RenderText(" : ", PUNC_BRUSH, this);
            if (_elseVM != null)
                _elseVM.Render(renderer, flags);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            if (_ifVM != null)
                _ifVM.RenderVisible(renderer, flags);
            if (_thenVM != null)
                _thenVM.RenderVisible(renderer, flags);
            if (_elseVM != null)
                _elseVM.RenderVisible(renderer, flags);
        }

        public override void UnRender()
        {
            if (_ifVM != null)
                _ifVM.UnRender();
            if (_thenVM != null)
                _thenVM.UnRender();
            if (_elseVM != null)
                _elseVM.UnRender();
            base.UnRender();
        }

        #endregion
    }
}
